<?php
require_once('functions.php');

// USER FUNCTIONS
function GetUsers(){
	$conn = GetConnection();
	return $results = $conn -> query('SELECT * FROM WSP2012_Users');
}

function GetUser($id){
	$conn= GetConnection();
	$sql = "SELECT * FROM WSP2012_Users WHERE users_id=$id ";
	$results = $conn -> query($sql);
	$row = $results -> fetch_assoc();
	$conn -> close();
	return $row;
}

function SaveUser($row){
	$conn = GetConnection();
	$row2 = EscapeRow($row, $conn);
	$sql = "UPDATE `WSP2012_Users` " 
		.	"Set usertype_id='$row2[usertype_id]', updated_at='$row2[updated_at]', email='$row2[email]', first_name='$row2[first_name]', "
		.	"last_name='$row2[last_name]', password='$row2[password]', birth_date='$row2[birth_date]' " 	
		.	"WHERE users_id='$row2[id]' ";
	$results = $conn -> query($sql);
	$error = $conn -> error;
	$conn -> close();
	
	return $error == '' ? true : array('SQL Error' => $error);	
}

function CreateUser($row){
	$conn = GetConnection();
	$row2 = EscapeRow($row, $conn);
	$sql = "INSERT INTO `WSP2012_Users` "
		.		"(users_id, usertype_id, created_at, updated_at, email, first_name, last_name, password, birth_date) "
		.		"VALUES ('$row2[users_id]', '$row2[usertype_id]', '$row2[created_at]', '$row2[updated_at]', '$row2[email]', '$row2[first_name]', '$row2[last_name]', '$row2[password]', '$row2[birth_date]') ";
	$results = $conn -> query($sql);
	$error = $conn -> error;
	$conn -> close();	
	
	return $error == '' ? true : array('SQL Error' => $error);
}

function DeleteUser($row, $id){
	$conn = GetConnection();
	$row2 = EscapeRow($row, $conn);
	$sql = "DELETE from WSP2012_Users WHERE users_id= '$row2[id]' ";
	$results = $conn -> query($sql);
	$error = $conn -> error;
	$conn -> close();	
	
	return $error == '' ? true : array('SQL Error' => $error);
}
?>
